﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using MiniStock.Entities.Common;

namespace MiniStock.Entities.Stock
{
    [DataContract(IsReference = true)]
    public class Order : EntityBase
    {
        [ForeignKey("Customer")]
        [DataMember]
        public Guid CustomerId { get; set; }
        [DataMember]
        public DateTime? OrderReceived { get; set; }
        [DataMember]
        public DateTime? OrderDeadline { get; set; }
        [DataMember]
        public DateTime? OrderShipped { get; set; }
        [DataMember]
        public List<OrderItem> Items { get; set; }

        [DataMember]
        [ForeignKey("CustomerId")]
        public Customer Customer { get; set; }

        public Order()
        {
            Items = new List<OrderItem>();
        }

        public override void CopyFrom(EntityBase source, int depthLimit = -1)
        {
            var item = (Order) source;

            base.CopyFrom(item, depthLimit);
            this.CustomerId = item.CustomerId;
            this.OrderReceived = item.OrderReceived;
            this.OrderDeadline = item.OrderDeadline;
            this.OrderShipped = item.OrderShipped;
            if (depthLimit != 0)
            {
                this.Items = item.Items.Select(i =>
                                                   {
                                                       var j = new OrderItem();
                                                       j.CopyFrom(i, depthLimit - 1);
                                                       return j;
                                                   }).ToList();
            }
        }

        #region Validators

        public static class Validator
        {
        }

        #endregion
    }

    public class OrderItem: EntityBase
    {
        public Guid OrderId { get; set; }
        [ForeignKey("Product")]
        public Guid ProductId { get; set; }
        public Decimal Amount { get; set; }

        [DataMember]
        [ForeignKey("ProductId")]
        public Product Product { get; set; }

        public override void CopyFrom(EntityBase source, int depthLimit)
        {
            var item = (OrderItem)source;

            base.CopyFrom(source, depthLimit);
            this.OrderId = item.OrderId;
            this.ProductId = item.ProductId;
            this.Amount = item.Amount;
        }
    }
}
